<template>
    <div>
        <div class="infoBlock">
            <div class="info">
                <div class="avatar">
                    <uploader class="uploader" :after-read="onRead">
                        <img :src="photo" alt="">
                    </uploader>
                    <div  class="national" >
                        <div  class="van-image" style="width: 20px;">
                            <img src="@as/img/national.7c84175e.png" class="van-image__img">
                        </div>
                    </div>
                </div>
                <div class="text">
                    <p class="name">
                        <span>{{name}}</span>
                        <img src="@as/img/dangyuan.png" alt="">
                        <span>{{party_posts}}</span>
                    </p>
                    <p class="com">{{organization}}</p>
                    <p class="date">{{party_time|time}} 入党</p>
                </div>
            </div>
            <ul class="nav">
                <li @click="goSubscription">
                    <img src="@as/img/wode_wddy@2x.png" alt>
                    <span>我的订阅</span>
                </li>
                <li @click="goCollection">
                    <img src="@as/img/wode_wdsc@2x.png" alt>
                    <span>我的收藏</span>
                </li>
                <li @click="goMyNotes">
                    <img src="@as/img/wode_wdbj@2x.png" alt>
                    <span>我的笔记</span>
                </li>
                <li @click="goReport">
                    <img src="@as/img/wode_sxhb@2x.png" alt>
                    <span>思想汇报</span>
                </li>
            </ul>
        </div>
        <ul class="MyNavList">
            <li @click="goUserInfo">
                <img src="@as/img/wode_grxx@2x.png" alt="">
                <span>个人信息</span>
                <img src="@as/img/wode_arrow.png" alt="">
            </li>
            <li @click="goOrganization">
                <img src="@as/img/wode_zbxx@2x.png" alt="">
                <span>支部信息</span>
                <img src="@as/img/wode_arrow.png" alt="">
            </li>
            <li @click="goAssessment">
                <img src="@as/img/wode_khxx@2x.png" alt="">
                <span>考核信息</span>
                <img src="@as/img/wode_arrow.png" alt="">
            </li>
            <li @click="goPartyPayment">
                <img src="@as/img/wode_dfjn@2x.png" alt="">
                <span>党费缴纳</span>
                <img src="@as/img/wode_arrow.png" alt="">
            </li>
            <li @click="goSetting">
                <img src="@as/img/wode_xtsz@2x.png" alt="">
                <span>系统设置</span>
                <img src="@as/img/wode_arrow.png" alt="">
            </li>
        </ul>
        <!--<div class="nation-page">-->
        <!--<div class="nation-option">-->
        <!--<p class="nation-title">配置小角标</p>-->
        <!--<ul>-->
        <!--<li class="active">-->
        <!--<img src="@as/img/national.7c84175e.png" alt="">-->
        <!--</li>-->
        <!--</ul>-->

        <!--</div>-->
        <!--</div>-->

    </div>
</template>

<script>
    import uploader from 'vant/lib/uploader';
    import 'vant/lib/uploader/style';
    import axios from 'axios';
    import html2canvas from 'html2canvas';
    export default {
        data() {
            return {
                name: '',
                photo: '',
                party_posts: '',
                organization: '',
                party_time: '',
                qiniuData: {
                    token: '',
                    key: ''
                },
            };
        },
        created(){
            let data = JSON.parse(Cookies.get('user_71ydj'))
            this.name = data.name
            this.photo = data.photo
            this.party_posts = data.party_posts
            this.organization = data.organization
            this.party_time = data.party_time
            this.getToken()
        },
        mounted() {
            this.$progress.done()
        },
        methods: {
            goUserInfo() {
                this.$router.push('/mine/UserInfo')
            },
            goMyNotes() {
                this.$router.push('/mine/MyNotes')
            },
            goAssessment(){
                this.$router.push('/home/bigData/assessment')
            },
            goPartyPayment() {
                this.$router.push('/mine/PartyPayment')
            },
            goSubscription(){
                this.$router.push('/learn/subscription')
            },
            goCollection() {
                this.$router.push('/mine/Collection')
            },
            goReport() {
                this.$router.push('/mine/report')
            },
            goOrganization(){
                this.$router.push('/mine/Organization')
            },
            goSetting(){
                this.$router.push('/mine/Setting')
            },
            S4() {
                return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
            },
            guid() {
                return (this.S4() + this.S4() + "-" + this.S4() + "-" + this.S4() + "-" + this.S4() + "-" + this.S4() + this.S4() + this.S4());
            },


            onRead(file) {
                let that = this;
                that.photo = file.content;
                console.log(file)
                this.qiniuData.key = this.guid()
                this.qiniuData.file = file.file;
                let param = new FormData(); //创建form对象
                param.append('chunk','0');//断点传输
                param.append('chunks','1');
                param.append('file',file.file,file.file.name)
                param.append('token',this.qiniuData.token)
                param.append('key',this.qiniuData.key)
                console.log(param.get('file')); //FormData私有类对象，访问不到，可以通过get判断值是否传
                const url = `https://up-z2.qiniup.com`

                html2canvas(document.querySelector('.avatar'), {async: false}).then(function (canvas) {
                    return new Promise((resolve, reject) => {
                        that.src = canvas.toDataURL("image/png");
                        var blob = dataURLtoBlob(that.src);
                        console.log(blob);
                        // var windowURL = window.URL || window.webkitURL;
                        // console.log(windowURL.createObjectURL(canvas.toDataURL("image/png")))
                        // console.log(that.src);
                        resolve(that.src)
                    })
                }).then((src)=>{
                    axios.post(url,param,{headers: {'Content-Type':'multipart/form-data'}}).then(res=>{
                        // let pic = `https://media.71ydj.com/${res.data.key}`
                        that.$http.post(`app/login/editphoto`,{
                            uid: JSON.parse(Cookies.get('user_71ydj')).id,
                            photo: src
                        }).then(res=>{
                            that.photo = src;
                            let data = JSON.parse(Cookies.get('user_71ydj'))
                            data.photo = pic;
                            Cookies.set('user_71ydj', data, { expires: 7 })
                        })
                    })

                })
                // console.log(html2canvas)
                // axios.post(url,param,{headers: {'Content-Type':'multipart/form-data'}}).then(res=>{
                //   let pic = `https://media.71ydj.com/${res.data.key}`
                //   that.$http.post(`app/login/editphoto`,{
                //     uid: JSON.parse(Cookies.get('user_71ydj')).id,
                //     photo: pic
                //   }).then(res=>{
                //     that.photo = pic
                //     let data = JSON.parse(Cookies.get('user_71ydj'))
                //     data.photo = pic
                //     Cookies.set('user_71ydj', data, { expires: 7 })
                //   })
                // })
            },
            getToken() {
                const url = `index/index/qiniu`
                this.$http.post(url).then(res => {
                    this.qiniuData.token = res.data.token
                })
            },
        },
        components: {
            uploader
        }
    };
</script>

<style scoped lang="scss">
    .infoBlock{
        position: relative;
        width: 100%;
        height: 232px;
        background: url(../../assets/img/wode_bg.png) center no-repeat;
        background-size: 100% 100%;
        box-sizing: border-box;
        padding: 70px 16px;
        margin-bottom: 56px;
        .info{
            display: flex;
            .avatar{
                width:61px;
                height:61px;
                background:rgba(244,244,244,1);
                border-radius: 6px;
                /*border-radius:50%;*/
                overflow: hidden;
                margin-right: 15px;
                margin-top: 3px;
                position: relative;
                .uploader{
                    width: 100%;
                    height: 100%;
                    img{
                        width: 100%;
                        height: 100%;
                    }
                }
                .national{
                    position: absolute;
                    right: 0;
                    bottom: 0;
                    line-height: 1;
                    height: 25px;
                }
                .van-image {
                    position: relative;
                    display: inline-block;
                }
                .van-image__error, .van-image__img, .van-image__loading {
                    display: block;
                    width: 100%;
                    height: 100%;
                }
            }
            .text{
                flex: 1;
                .name{
                    display: flex;
                    align-items: baseline;
                    margin-bottom: 12px;
                    span{
                        &:first-of-type{
                            font-size:17px;
                            line-height: 17px;
                            font-family:PingFang-SC-Medium;
                            font-weight:500;
                            color:rgba(255,255,255,1);
                        }
                        &:last-of-type{
                            font-size:12px;
                            line-height: 12px;
                            font-family:PingFang-SC-Medium;
                            font-weight:500;
                            color:rgba(255,255,255,1);
                        }
                    }
                    img{
                        width:10px;
                        height:12px;
                        margin-left: 9px;
                        margin-right: 5px;
                    }
                }
                .com{
                    font-size:12px;
                    line-height: 12px;
                    font-family:PingFang-SC-Medium;
                    font-weight:500;
                    color:rgba(255,255,255,1);
                    margin-bottom: 15px;
                }
                .date{
                    font-size:12px;
                    line-height: 12px;
                    font-family:PingFang-SC-Medium;
                    font-weight:500;
                    color:rgba(255,255,255,.6);
                }
            }
        }
        .nav{
            position: absolute;
            left: 50%;
            transform: translateX(-50%);
            bottom: -34px;
            width:330px;
            height:100px;
            background:rgba(255,255,255,1);
            box-shadow:3px 3px 10px 0px rgba(51,51,51,0.08);
            border-radius:6px;
            display: flex;
            justify-content: center;
            li{
                height: 100%;
                display: flex;
                flex-direction: column;
                list-style: none;
                align-items: center;
                justify-content: center;
                font-size:12px;
                font-family:PingFang-SC-Medium;
                font-weight:500;
                color:rgba(51,51,51,1);
                line-height:10px;
                margin-right: 27px;
                &:last-of-type{
                    margin: 0;
                }
                img{
                    width: 46px;
                    height: 46px;
                    margin-bottom: 5px;
                }
            }
        }
    }
    .MyNavList{
        box-sizing: border-box;
        padding: 0 23px;
        padding-bottom: 70px;
        li{
            display: flex;
            justify-content: space-between;
            align-items: center;
            height: 52px;
            font-size:14px;
            font-family:PingFang-SC-Medium;
            font-weight:500;
            color:rgba(51,51,51,1);
            img{
                &:first-of-type{
                    width:15px;
                    height:15px;
                    margin-right: 8px;
                }
                &:last-of-type{
                    width:6px;
                    height:10px;
                }
            }
            span{
                flex:1;
            }
        }
    }
    .nation-page{
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background: rgba(0,0,0,0.4);
        z-index: 9999;

        .nation-option{
            position: absolute;
            left: 0;
            top: 0;
            right: 0;
            bottom: 0;
            margin: auto;
            width: 350px;
            height: 400px;
            background: white;
            border-radius: 5px;
            .nation-title{
                text-align: center;
                font-size: 14px;
                color: #333;
                margin: 10px 0;
            }
            ul{
                height: 300px;
                overflow-y: scroll;
                li{
                    width: 25%;
                    height: 80px;
                    text-align: center;
                    position: relative;
                    &.active{
                        &:after{
                            position: absolute;
                            right: 0;
                            top:0 ;
                            content: '';
                            width: 20px;
                            height: 20px;
                            border-radius: 50%;
                            background: rgba(0,0,0,0.4);
                        }
                    }

                    img{
                        width: 80px;
                        height: 80px;
                        border-radius: 50%;
                    }
                }
            }
        }
    }

</style>